#!/usr/bin/env python

"""IMAP module, which encapsulate a connection to an IMAP4 server.
   It implement the subset of the imaplib from the python library.
"""

__author__ = 'Yong Liu'
__version__ = '0.1'
__date__ = '2010-07-27'

import imaplib

class imap4:

    """imap4 client class.

       Create a connection to an IMAP4 server.

       Instantiate with: imap4([host[, protocol[, port]]])
       
               host - host's name (default: localhost);
               protocol - IMAP4 or IMAP4S (default: IMAP4);
               port - port number (default: standard IMAP4 port 143).
               
       Support all IMAP4rev1 commands.
    """

    def __init__(self, host = 'localhost', protocol = 'IMAP4', port = 143):
        """Create a connection to the server on "host:port"
           default: localhost:standard IMAP4 port.
        """
        self.host = host
        self.protocol = protocol
        self.port = port

        if self.protocol == 'IMAP4':
            self.connection = imaplib.IMAP4(self.host, self.port)
        else:
            self.connection = imaplib.IMAP4_SSL(self.host, self.port)

    def login(self, account, password):
        """User authentication
        """
        typ, data = self.connection.login(account, password)
        
        if typ == 'OK':
            return True
        else:
            return False

    
